library(tidyverse)
library(foreign)
library(scales)

rm(list=ls())


setwd("Yourpath")

#############################################################
# Figure 1: Average Turnout in Referendums by  Postal Voting
# Before and After the Introduction of Postal Voting, 1981-2009 
# (Canton-level Referendum Data)
#############################################################


data <- read.dta("./partrends/parallel_trends_all.dta",convert.factors = F)

difference <- mean(data$turnout_treatmentgroup[is.element(data$abstag_rel_canton,c(-5:-1))]-data$turnout_controlgroup[is.element(data$abstag_rel_canton,c(-5:-1))])


df <- tibble(x=c(data$abstag_rel_canton,data$abstag_rel_canton,data$abstag_rel_canton),
                 y=c(data$turnout_treatmentgroup,data$turnout_controlgroup,c(data$turnout_treatmentgroup-data$turnout_controlgroup-difference)),
                 timevariable=c(data$time_var1,data$time_var1,data$time_var1),
                 canton=c(data$kkurz,data$kkurz,data$kkurz),
                 group=c(matrix(1,length(data$turnout_controlgroup),1),matrix(2,length(data$turnout_controlgroup),1),matrix(3,length(data$turnout_controlgroup),1)))

dfc <- df %>% filter(!canton %in% c("TI","VD","VS","NE","SZ") & is.na(timevariable)==F)%>%  # exclude JU and other cantons that introduced postal voting relatively late (b/c we lose post-treatment observations if we include them)
             group_by(group,timevariable) %>%
             group_by(N = n(), add = TRUE) %>% 
             select(-canton)%>% 
             summarise_all(funs(mean, max, sd), na.rm = TRUE) %>%
             mutate(se=y_sd/sqrt(N)) %>%
             rename(y=y_mean) %>%
             select(group,timevariable,y,se) 


ggplot(dfc, aes(x=timevariable,y=y/100)) + 
  geom_line(size=1,aes(x=timevariable,y=y/100,color=factor(group),shape=factor(group))) + 
  geom_point(size=2.5,aes(x=timevariable,y=y/100,color=factor(group),shape=factor(group))) +
  geom_errorbar(aes(x=timevariable,y=y/100,ymin = y/100 - 1.96*se/100,colour=factor(group),ymax = y/100 + 1.96*se/100 ),width=0.2) +   
  scale_y_continuous(labels = percent,limits=c(-0.02,1.0)) + 
  theme_bw(base_size = 28)  + 
  scale_x_continuous(limits = c(-5.5,4.5),breaks=c(-5:4) ) +
  geom_segment(aes(x = 1.0, y = 0.8, xend = -0.5, yend = 0.8), arrow = arrow(length = unit(0.5, "cm")),colour="black") +
  annotate("text", x = 2.0, y = 0.8, label = "Introduction of \n postal voting  ",size=8) +
  xlab("Event time") + ylab("Turnout") +
  scale_color_manual(values = c("black","gray40","black"),label=c("Treatment group  ","Control group  ", "Effect of postal voting (in % points)")) +
  scale_shape_manual(values = c(1,2,4),label=c("Treatment group  ","Control group  ", "Effect of postal voting (in % points)")) +
  scale_linetype_manual(values = c(1,2,3),label=c("Treatment group  ","Control group  ", "Effect of postal voting (in % points)")) +
  theme(legend.title = element_blank())  +
  theme(legend.direction = "horizontal", legend.position = "bottom") +
  geom_vline(xintercept = -0.5,linetype="dashed")

ggsave(file="Figure1.pdf",width=15,height=10 )

